<template>
  <div class="search">
    <van-search
      v-model="value"
      show-action
      shape="round"
      placeholder="请输入搜索关键词"
    >
      <template #action>
        <div @click="onSearch">搜索</div>
      </template>
    </van-search>

    <div class="hottext">
      <h4>热门搜索</h4>
      <ul>
        <li
          v-for="(item, index) in hottext"
          :key="index"
          @click="showHotText(item)"
        >
          {{ item }}
        </li>
      </ul>
    </div>

    <div class="container" v-if="!showNoGood">
      <div class="recommend">
        <div class="rec_good" v-for="item in hotList" :key="item.id">
          <router-link :to="`/detail/${item.id}`">
            <div class="rec_left">
              <van-image fit="contain" :src="item.image" alt=""></van-image>
            </div>
            <div class="rec_right">
              <div class="rec_title">{{ item.store_name }}</div>
              <div class="rec_price"><span>¥</span>{{ item.price }}</div>
              <div class="rec_last">
                <div class="rec_vip">¥{{ item.vip_price }}</div>
                <div class="rec_sale">已售{{ item.sales }}件</div>
              </div>
            </div>
            <div class="rec_cart">
              <van-icon name="shopping-cart-o" color="#fc4141" />
            </div>
          </router-link>
        </div>
      </div>
    </div>

    <div class="container2" v-if="showNoGood">
      <NoGood></NoGood>
    </div>
  </div>
</template>

<script>
import NoGood from "../components/NoGood.vue";
export default {
  name: "Search",
  data() {
    return {
      value: "",
      hottext: ["吸尘器", "加湿器", "马桶", "热水器"],
      hotList: null,
      showNoGood:false
    };
  },
  methods: {
    onSearch() {
      // console.log(this.value);
      this.$axios
        .get("api/products?page=1&limit=20&keyword=" + this.value)
        .then((d) => {
          this.hotList = d.data.data;
          // console.log(this.hotList);
          if(this.hotList.length == 0){
            this.showNoGood = true
          }else{
            this.showNoGood = false
          }
        });
    },
    showHotText(text) {
      this.value = text;
      this.onSearch();
    },
  },
  components: {
    NoGood,
  },
};
</script>

<style lang="less">
.search {
  .hottext {
    padding: 0rem 1rem;
    h4 {
      font-size: 0.9rem;
      color: #a6a6a6;
      font-weight: normal;
    }
    ul {
      li {
        float: left;
        font-size: 0.9rem;
        color: #3f3f3f;
        padding: 0.2rem 0.5rem;
        border: 1px solid #b4b4b4;
        margin-right: 0.8rem;
      }
    }
  }
  .container {
    padding: 0rem 1rem;
    margin-top: 4.5rem;
    .recommend {
      .rec_good {
        position: relative;
        margin-bottom: 1rem;
        & > a {
          color: black;
          display: flex;
          padding: 0.5rem 0px;
          height: 5.5rem;
          .rec_left {
            width: 5.5rem;
            .van-image {
              height: 100%;
              width: 100%;
              img {
                height: 5.5rem;
                width: 5.5rem;
              }
            }
          }
          .rec_right {
            overflow: hidden;
            margin-left: 1rem;
            width: calc(100% - 7rem);
            .rec_title {
              white-space: nowrap;
              text-overflow: ellipsis;
              overflow: hidden;
            }
            .rec_price {
              color: #fc4141;
              font-size: 1.1rem;
              margin-top: 1.5rem;
              span {
                font-size: 0.8rem;
                margin-right: 0.2rem;
              }
            }
            .rec_last {
              font-size: 0.8rem;
              display: flex;
              align-items: center;
              margin-top: 0.4rem;
              .rec_vip {
                position: relative;
                &:after {
                  content: "";
                  background: url();
                  height: 0.8rem;
                  width: 2rem;
                  position: absolute;
                  top: 0px;
                  background-repeat: no-repeat;
                  background-size: 1.8rem 0.8rem;
                }
              }
              .rec_sale {
                margin-left: 3rem;
                color: #7f7f7f;
              }
            }
          }
          .rec_cart {
            position: absolute;
            right: 0px;
            bottom: 0.6rem;
            height: 1.5rem;
            width: 1.5rem;
            line-height: 1.75rem;
            text-align: center;
            border-radius: 50%;
            border: 1px solid #fc4141;
            .rec_cart {
              .van-icon {
              }
            }
          }
        }
      }
    }
  }
}
</style>